package io.renren.modules.pms.controller;

import io.renren.common.entity.GoodsInfoEntity;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.pms.dto.GoodsInfoDTO;
import io.renren.modules.pms.service.GoodsInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 商品表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2024-08-13 15:18:06
 */
@RestController
@RequestMapping("pms/goodsinfo")
public class GoodsInfoController {
    @Autowired
    private GoodsInfoService goodsInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("pms:goodsinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsInfoService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("pms:goodsinfo:info")
    public R info(@PathVariable("id") Integer id){
        GoodsInfoEntity goodsInfo = goodsInfoService.getById(id);
        return R.ok().put("goodsInfo", goodsInfo);
    }



    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("pms:goodsinfo:save")
    public R save(@RequestBody GoodsInfoDTO goodsInfo){
        ValidatorUtils.validateEntity(goodsInfo);
        GoodsInfoEntity goodsInfo1 = ConvertUtils.sourceToTarget(goodsInfo, GoodsInfoEntity.class);
        goodsInfoService.save(goodsInfo1);
        return R.ok();
    }


    /**
     * 启用或禁用商品
     *
     * @return
     */
    @PostMapping("/status/{status}")
    @RequiresPermissions("pms:goodsinfo:status")
    @ApiOperation("上架或下架商品")
    public R startOrStop(@PathVariable Integer status, Long id) {
        goodsInfoService.startOrStop(id, status);
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("pms:goodsinfo:update")
    public R update(@RequestBody GoodsInfoDTO goodsInfoDTO){
        ValidatorUtils.validateEntity(goodsInfoDTO);

        goodsInfoService.updateInfo(goodsInfoDTO);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("pms:goodsinfo:delete")
    public R delete(@RequestBody Integer[] ids){
        goodsInfoService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

}
